package com.dlxj.action;

import java.util.HashMap;
import java.util.Iterator;
import java.util.List;

import com.dlxj.entity.Bugtype;
import com.dlxj.service.BugTypeService;
import com.dlxj.util.MyJSON;

public class BugTypeAction {
	Integer bid;

	public Integer getBid() {
		return bid;
	}

	public void setBid(Integer bid) {
		this.bid = bid;
	}

	String json;
	Bugtype bugtype;
	BugTypeService bugTypeService;
	String numsIdList;

	public String addBugType() {
		int code = bugTypeService.addBugType(bugtype);
		json = "{code:" + code + "}";
		return "success";

	}

	public String findList() {

		List<Bugtype> bts = bugTypeService.findBugTypeList();
		json = MyJSON.toJson(bts).toString();
		return "success";
	}

	public String updateName() {

		int code = bugTypeService.modifyBugType(bugtype.getId(),
				bugtype.getName());
		json = "{code:" + code + "}";
		return "success";
	}

	public String getJson() {
		return json;
	}

	public void setJson(String json) {
		this.json = json;
	}

	public Bugtype getBugtype() {
		return bugtype;
	}

	public void setBugtype(Bugtype bugtype) {
		this.bugtype = bugtype;
	}

	public BugTypeService getBugTypeService() {
		return bugTypeService;
	}

	public void setBugTypeService(BugTypeService bugTypeService) {
		this.bugTypeService = bugTypeService;
	}

	public String deleteBugType() {
		int code = bugTypeService.deleteBugType(bid);
		json = "{code:" + code + "}";
		return "success";
	}

	public String deleteBugTypeByAll() {
		int code = 0;
		HashMap map1 = new HashMap();
		System.out.println(numsIdList);
		/* 循环使用《，》拆分接收的字符串; */
		String a[] = numsIdList.split(",");
		for (int i = 0; i < a.length; i++) {
			int co = bugTypeService.findBugTypeById(Integer.parseInt(a[i]));
			map1.put(a[i], co);
		}
		/* 判断是否包含不能删除的选项 */
		boolean flag = map1.containsValue(1);
		if (flag) {
			/*
			 * 如果有不能删除的选项 直接提示删除失败
			 */
			json = "{code:" + code + "}";
			return "success";
		} else {
			/*
			 * 没有不能删除的选项 循环map里存放的key(编号id)并逐个删除
			 */
			Iterator keys = map1.keySet().iterator();
			while (keys.hasNext()) {
				String key = (String) keys.next();
				code = bugTypeService.deleteBugType(Integer.parseInt(key));
			}
		}
		json = "{code:" + code + "}";
		return "success";
	}

	public String getNumsIdList() {
		return numsIdList;
	}

	public void setNumsIdList(String numsIdList) {
		this.numsIdList = numsIdList;
	}

}
